//
//  FQDRegularArticleFeedCell.m
//  FakeQDaily
//
//  Created by mewxiv on 8/15/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDRegularArticleFeedCell.h"
#import "FQDRestModel.h"
#import "UIButton+FQDExtension.h"
#import "NSDate+FQDExtension.h"
#import "FQDArticleFeedCellItem.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface FQDRegularArticleFeedCell ()



@end

@implementation FQDRegularArticleFeedCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDBoldFontWithSize(15.0f);
    _titleLabel.textColor = FQDFeedTitleTextColor;
    _titleLabel.textAlignment = NSTextAlignmentNatural;
    _titleLabel.numberOfLines = 3;
    _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _titleLabel.preferredMaxLayoutWidth = 140.0f;
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.clipsToBounds = YES;
    [self.contentView addSubview:_titleLabel];
    
    _categoryLabel = [[UILabel alloc] init];
    _categoryLabel.font = FQDRegularFontWithSize(12.0f);
    _categoryLabel.textColor = FQDFeedDetailTextColor;
    _categoryLabel.backgroundColor = [UIColor whiteColor];
    _categoryLabel.clipsToBounds = YES;
    [self.contentView addSubview:_categoryLabel];
    
    _commentButton = [UIButton commentButtonWithCommentNum:10];
    _commentButton.titleLabel.backgroundColor = [UIColor whiteColor];
    _commentButton.titleLabel.clipsToBounds = YES;
    [self.contentView addSubview:_commentButton];
    
    _praiseButton = [UIButton praiseButtonWithPraiseNum:27];
    _praiseButton.titleLabel.backgroundColor = [UIColor whiteColor];
    _praiseButton.titleLabel.clipsToBounds = YES;
    [self.contentView addSubview:_praiseButton];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = FQDRegularFontWithSize(12.0f);
    _dateLabel.textColor = FQDFeedDetailTextColor;
    _dateLabel.backgroundColor = [UIColor whiteColor];
    _dateLabel.clipsToBounds = YES;
    [self.contentView addSubview:_dateLabel];
    
    
    UIImage *descImage = [[UIImage alloc] init];
    _descImageView = [[UIImageView alloc] init];
    _descImageView.contentMode = UIViewContentModeScaleAspectFill;
    _descImageView.clipsToBounds = YES;
    _descImageView.image = descImage;
    [self.contentView addSubview:_descImageView];
    
}

- (void)prepareConstraints
{
    WeakSelf;

    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(weakSelf);
        make.height.equalTo(@(115));
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(15);
        make.left.equalTo(weakSelf.contentView).offset(10);
        make.bottom.lessThanOrEqualTo(weakSelf.categoryLabel.mas_top).offset(-10);
    }];

    [_descImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView);
        make.right.equalTo(weakSelf.contentView);
        make.width.equalTo(weakSelf.contentView).multipliedBy(0.5);
        make.height.equalTo(weakSelf.contentView);
    }];
}

- (void)remakeConstraints:(FQDFeed *)feed
{
    WeakSelf;

    BOOL commentHidden = (feed.post.comment_count <= 0);
    BOOL praiseHidden = (feed.post.praise_count <= 0);
    BOOL isTooLong = (feed.post.comment_count >= 10 || feed.post.praise_count >= 10);
    isTooLong = (isTooLong && ([self.dateLabel.text length] > 4));
    UIView *leftView = self.categoryLabel;
    
    
    [_categoryLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLabel.mas_left);
        if (!isTooLong) {
            make.bottom.equalTo(weakSelf.contentView).offset(-7);
        }
    }];
    
    _commentButton.hidden = commentHidden;
    [_commentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftView.mas_right).offset(6);
        make.bottom.equalTo(leftView.mas_bottom);
    }];

    leftView = (commentHidden) ? leftView : _commentButton;
    _praiseButton.hidden = praiseHidden;
    [_praiseButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftView.mas_right).offset(10);
        make.bottom.equalTo(weakSelf.categoryLabel.mas_bottom);
    }];

    leftView = (praiseHidden) ? leftView : _praiseButton;
    if (isTooLong) {
        [_dateLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.categoryLabel.mas_bottom);
            make.left.equalTo(weakSelf.titleLabel.mas_left);
            make.bottom.equalTo(weakSelf.contentView).offset(-7);
        }];
    } else {
        [_dateLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(leftView.mas_right).offset(8);
            make.bottom.equalTo(weakSelf.categoryLabel);
        }];
    }
}

- (void)configureCellWithCellItem:(FQDArticleFeedCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDFeed *feed = cellItem.feed;
    
    [self.titleLabel setText:feed.post.title];
    [self.categoryLabel setText:feed.post.category.title];
    [self.commentButton setTitle:[NSString stringWithFormat:@"%@", @(feed.post.comment_count)]
                        forState:UIControlStateNormal];
    [self.praiseButton setTitle:[NSString stringWithFormat:@"%@", @(feed.post.praise_count)]
                       forState:UIControlStateNormal];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:feed.post.publish_time];
    [self.dateLabel setText:[date dateStringSinceNow]];
    
    [self.descImageView sd_setImageWithURL:[NSURL URLWithString:feed.image]];
    
    [self remakeConstraints:feed];
}


#pragma mark - Getters and Setters
+ (NSString *)reuseId
{
    return @"RegularArticleFeedCellId";
}


@end
